
package net.swa.system.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import net.swa.system.beans.entity.Dict;
import net.swa.system.dao.HibernateDaoSupport;
import net.swa.system.service.MerchantsTypeService;
import net.swa.util.JsonResult;

@Service("merchantsTypeService")
public class MerchantsTypeServiceImpl extends HibernateDaoSupport implements MerchantsTypeService
{

    @Override
    public Map<String, Object> delete(Long id)
    {
        Map<String, Object> r = new HashMap<String, Object>();
        Session session = getCurrentSession();
        Dict d = (Dict) session.get(Dict.class, id);
        if (null != d)
        {
            String hql = "from  Merchants  where type=:type and deldata=:deldata";
            Query q = session.createQuery(hql);
            q.setString("type", d.getValue());
            q.setBoolean("deldata", false);
            List<?> l = q.list();
            if (null != l && l.size() > 0)
            {
                r.put("success", false);
                r.put("message", "有该类型商家，不可以删除");
            }
            else
            {
                session.delete(d);
                r.put("success", true);
                r.put("message", "删除成功");
            }
        }
        else
        {
            r.put("success", false);
            r.put("message", "该记录已经不存在");
        }
        return r;
    }

    @Override
    public Map<String, Object> updateDicNum(Long[] ids , Long[] num)
    {
        Map<String, Object> r = new HashMap<String, Object>();
        for (int i = 0; i < ids.length; i++)
        {
            Query query = getCurrentSession().createQuery("update Dict" + " set dictPaixu=:num where id=:id");
            query.setLong("num", num[i]);
            query.setLong("id", ids[i]);
            query.executeUpdate();
        }
        r.put("success", true);
        r.put("message", "更新成功");

        return r;
    }

    @Override
    public Map<String, Object> addOrUpdate(Dict m)
    {
        Map<String, Object> r = new HashMap<String, Object>();
        if (null != m)
        {
            //value 和key相同
            m.setKey(m.getValue());

            Session session = getCurrentSession();
            if (0 == m.getId())
            {
                session.save(m);
                r.put("success", true);
                r.put("message", "新增成功");
            }
            else
            {
                Dict b = (Dict) session.get(Dict.class, m.getId());
                if (null != b)
                {
                    if (b.getValue().equals(m.getValue()))
                    {
                        r.put("success", true);
                        r.put("message", "更新成功");
                    }
                    else
                    {
                        String hql = "update Merchants set type=:type where type=:type2 ";
                        Query q = session.createQuery(hql);
                        q.setString("type", m.getValue());
                        q.setString("type2", b.getValue());
                        q.executeUpdate();
                        
                        b.setKey(m.getKey());
                        b.setValue(m.getValue());
                        session.update(b);
                        r.put("success", true);
                        r.put("message", "更新成功");

                    }
                }
                else
                {
                    r.put("success", false);
                    r.put("message", "系统错误");
                }

            }
        }
        else
        {
            r.put("success", false);
            r.put("message", "系统错误");
        }

        return r;
    }

}
